//禁用地图的 UI
[] spawn {
	disableSerialization;
	waitUntil {!isNull(findDisplay 12)};
	((finddisplay 12) displayctrl 1202) ctrlSetText "icon_noplayer_ca.paa";
	((finddisplay 12) displayctrl 1202) ctrlEnable false;
	((finddisplay 12) displayctrl 1202) ctrlSetTooltip "请用地图和指南针定位";
	
	((finddisplay 12) displayctrl 101) ctrlshow false;
	((finddisplay 12) displayctrl 101) ctrlEnable false;
};

titleText ["加载中……", "BLACK", 0.01];


//
if(side player == east) then
{
	["InitializePlayer", [player, true]] call BIS_fnc_dynamicGroups;
};
//--------------------------Info 介绍----------------------------------------------
[] execVM "info.sqf";

//任意穿衣服

//_____________________________ Uniform Script End 2nd Update 19/03/18 _____________________________
fnc_otherUnif = {
  params ["_type","_return"];
  if (_type == 0) exitWith {
    MGI_button = _return select 1; false
  };
  if (_type == 1 and !isnil "MGI_button" && {MGI_button == 1}) then {
    disableSerialization;
    _control = _return select 0;
    _index = _return select 1;
    private _idc = ctrlIDC _control;
    private _text = lbText [_idc, _index];
    private _picture = lbPicture [_idc,_index];
    private _uniforms = ("getText (_x >> 'displayName') == _text && getNumber (_x >> 'ItemInfo' >> 'type') == 801" configClasses (configFile >> "CfgWeapons"));
    if (count _uniforms > 0) then {
      [_idc,_text,_picture,_index,_uniforms] spawn {
        params ["_idc","_text","_picture","_index","_uniforms","_selectedUnif","_unifContClasses",["_uniformObject",objNull],["_selectedInvent",[]],"_items","_otherItems","_g0"];
        _currentUnif = uniform player;
        _currentInvent = uniformItems player;
        _selectedUnif = ((_uniforms select {toLower (gettext (_x >> "picture")) splitString "\"joinString "\" == _picture}) apply {configName _x}) select 0;

        if !(pl_container isKindOf "CAManBase") then {
          _unifContClasses = (everyContainer pl_container select { (_x select 0) select [0,2] == "U_"});
          _cnt = 0;
          for "_i" from 0 to _index do {
            if (lbText [_idc,_i] == _text) then {
              _cnt = _cnt +1;
              _uniformObject = _unifContClasses select {_selectedUnif == (_x select 0)} select (_cnt -1) select 1
            };
          };
        } else {
          _uniformObject = uniformContainer pl_container
        };
        _selectedInvent = [];
        {_selectedInvent pushback _x} foreach (itemCargo _uniformObject);
        if (magazineCargo _uniformObject isEqualType []) then {
          {_selectedInvent pushback _x} foreach (magazineCargo _uniformObject);
        };
        if !(pl_container isKindOf "CAmanbase") then {
          _items = +itemCargo pl_container;
          _unifItems = _unifContClasses apply {_x select 0};
          _vestContClasses = (everyContainer pl_container select { (_x select 0) select [0,2] == "V_"});
          _vestItems = _vestContClasses apply {_x select 0};
          _otherItems = +_items - _unifItems - _vestItems;
          pl_container setVariable ["allconts",_unifContClasses+_vestContClasses];
          for "_i" from 0 to count (_unifContClasses+_vestContClasses) -1 do {
            if (((pl_container getVariable "allconts") select _i) select 1 == _uniformObject) exitWith {
              (pl_container getVariable "allconts") deleteAt _i
            };
          };
          pl_container setVariable ["allconts", +(pl_container getVariable "allconts") apply {[_x select 0, itemCargo (_x select 1), magazineCargo (_x select 1)]}];
          _g0 = pl_container;
          clearItemCargoGlobal _g0;
        } else {
          _g0 = createVehicle ["WeaponHolderSimulated_Scripted", (player modelToWorld [0,1,1]), [], 0, "CAN_COLLIDE"]
        };
        player forceAddUniform _selectedUnif;
        {player addItemToUniform _x} forEach _selectedInvent;
        call {
          if (pl_container isKindOf "CAManBase") exitWith {
            removeUniform pl_container
          };
          {
            _x params ["_cont",["_it",[]],["_mag",[]]];
            pl_container addItemCargoGlobal [_cont,1];
            _createdCont = pl_container call MGI_lastCont;
            {_createdCont addItemCargoGlobal [_x,1];true} count _it;
            {_createdCont addMagazineCargoGlobal [_x,1];true} count _mag;
          } forEach (pl_container getVariable ["allconts",[]]);
          {_g0 addItemCargoGlobal [_x,1]} forEach _otherItems;
        };
        _g0 addItemCargoGlobal [_currentUnif,1];
        _lastCont = _g0 call MGI_lastCont;
        {_lastCont addItemCargoGlobal [_x,1]} forEach _currentInvent;
        if (_g0 !=pl_container && {(count itemCargo _g0 + count magazineCargo _g0 + count weaponCargo _g0 + count backpackCargo _g0) == 0}) then {
          deleteVehicle _g0
        };
      };
    };
  };
  MGI_button = 0; false
};

MGI_lastCont = compileFinal "
  params [['_cont',objNull]];
  private '_lastCont'; 
   _invent = (everyContainer _cont - (everyBackpack _cont apply {[typeOf _x,_x]}));
   if !(_invent isEqualTo []) then {
    _lastCont = _invent select (count _invent -1) select 1;
   } else {
    _lastCont = _cont;
   };
   _lastCont
";
 
player addEventHandler ["InventoryOpened", { 
  params ["_unit", "_container"]; 
  pl_container = _container; 
  [] spawn { 
    waitUntil {!(isNull findDisplay 602)}; 
    disableSerialization; 
    {(findDisplay 602 displayCtrl _x) ctrlAddEventHandler ["MouseButtonClick", "[0,_this] call fnc_otherUnif"]} forEach [632,640];
    {(findDisplay 602 displayCtrl _x) ctrlAddEventHandler ["LBSelChanged", "[1,_this] spawn fnc_otherUnif"]} forEach [632,640]; 
  }; 
}];
//_____________________________ Uniform Script End 2nd Update 19/03/18 _____________________________


//设置玩家拥有其他兵种特性
player setUnitTrait ["UAVHacker", true];
player setUnitTrait ["explosiveSpecialist", true];


//_____________________________ 弹夹整理 _____________________________
[] execVM "Scripts\outlw_magRepack\MagRepack_init_sv.sqf";


//_____________________________ AL手雷 _____________________________
// by ALIAS
waitUntil {!isNil "enable_emp_grenade"};
if (enable_toxic_grenade) then {execVM "AL_grenades\al_ini_toxic.sqf"};
if (enable_incendiary_grenade) then {execVM "AL_grenades\al_ini_incendiary.sqf"};

waitUntil {!isNil "allPlayers_on"};

player addEventHandler ["Fired",{private ["_al_throwable"]; _al_throwable = _this select 6; _shooter = _this select 0;
[_al_throwable,_shooter,"Chemlight_red","Chemlight_blue","Chemlight_green","Chemlight_yellow"] execvm "AL_grenades\al_grenade.sqf"}];


//_____________________________ 自动跑步 _____________________________
[] execVM "Scripts\repeatPlyAnim.sqf";


//_____________________________ 无线电效果 _____________________________
[]spawn fn_radioEffect;




//等待直到服务器初始化完成
waituntil
{
sleep 1;
(!isNil "as_server_done")
 || (side player != east)
};


//加载变量
[player] remoteExec ["fn_onPlayerJoin", 2];
waituntil{
	sleep 1;
	player getVariable ["as_init", false]
};

sleep 4;
titleFadeOut 0.5;

//设置rank
fnc_setPlayerRank = {
	if(_this >= 20) exitWith{player setRank "MAJOR"};
	if(_this >= 15) exitWith{player setRank "CAPTAIN"};
	if(_this >= 10) exitWith{player setRank "LIEUTENANT"};
	if(_this >= 5) exitWith{player setRank "SERGEANT"};
	if(_this >= 1) exitWith{player setRank "CORPORAL"};
	player setRank "PRIVATE";
};
private _winCount = player getVariable ["as_win", 0];
_winCount call fnc_setPlayerRank;


sleep 5;

//位置显示
[] spawn {
	["斩首行动2.1", format["位于 %1", worldName] , str (date select 2) + "/" + str (date select 1) + "/" + str (date select 0) + " " + str (date select 3) + ":00"] spawn BIS_fnc_infoText;
};


//任务
[] spawn fn_initTask;